package com.example.x1c.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private IntentFilter intentFilter;
    
    private LocalReceiver localReceiver;

    private LocalBroadcastManager localBroadcastManager;

    //private NetWorkChangeReceiver netWorkChangeReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        localBroadcastManager = LocalBroadcastManager.getInstance(this); // 获取实例
//        intentFilter = new IntentFilter();
//        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        netWorkChangeReceiver = new NetWorkChangeReceiver();
//        registerReceiver(netWorkChangeReceiver, intentFilter);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent("com.example.x1c.broadcasttest.MY_BROADCAST");
                Intent intent = new Intent("com.example.x1c.broadcasttest.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent);// 本地广播
                sendOrderedBroadcast(intent, null);

            }
        });

        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.x1c.broadcasttest.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, intentFilter); // 注册本地广播监听器



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }

    class LocalReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "LocalReceiver", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onReceive: LocalReceiver");
        }
    }

//    class NetWorkChangeReceiver extends BroadcastReceiver{
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//            if(networkInfo != null && networkInfo.isAvailable()){
//                Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
//            }else{
//                Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
//            }
//
//
//
//
//
//        }
//    }
}
